//
//  GQRankVewController.m
//  MyMusic
//
//  Created by sunny on 16/4/3.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "GQRankVewController.h"
#import "GQRankItem.h"
#import "GQRankCell.h"
#import "GQRankTwoLevelViewController.h"
static NSString *ID = @"Cell";
@interface GQRankVewController ()
@property (nonatomic,strong)NSArray *itemArray;
@end

@implementation GQRankVewController

- (NSArray *)itemArray
{
    if (!_itemArray) {
        _itemArray = [NSArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 150;
    
//    注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"GQRankCell" bundle:nil]  forCellReuseIdentifier:ID];
    

    
//    设置尾部视图
    UILabel *label = [[UILabel alloc] init];
    label.text = @"没有更多数据了，无法上拉";
    label.gq_height = 20;
    label.textAlignment = UITextAlignmentCenter;
    label.hidden = YES;
    self.tableView.tableFooterView = label;
    
//    下拉刷新的时候，请求数据
    self.tableView.mj_header = [GQDIYRefreshHeader gq_headerWithRefreshingBlock:^{
//        加载数据
        [self loadData];
    }];
//    一开始就加载数据
    [self.tableView.mj_header beginRefreshing];
    
//    让第一个cell的y值为10
    self.tableView.contentInset = UIEdgeInsetsMake(GQGlobalMagin - 35, 0, 0, 0);
}

#pragma mark - 加载数据
- (void)loadData
{
    [GQHttpRequest GET:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.7.2.0&channel=xiaomi&operator=2&method=baidu.ting.billboard.billCategory&format=json&kflag=1" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        self.itemArray = [GQRankItem mj_objectArrayWithKeyValuesArray:responseObject[@"content"]];
        
//        刷新表格
        [self.tableView reloadData];
        
//        显示底部视图
        self.tableView.tableFooterView.hidden = NO;
        
//        结束下拉刷新
        [self.tableView.mj_header endRefreshing];
       
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //        结束下拉刷新
        [self.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];

    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.itemArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GQRankCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    GQRankItem *rankItem = self.itemArray[indexPath.row];
    cell.rankItem = rankItem;
    
    return cell;
}

#pragma mark - 选择行，跳转到对应的控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GQRankCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    GQRankTwoLevelViewController *vc = [[UIStoryboard storyboardWithName:@"GQRankTwoLevelViewController" bundle:nil] instantiateInitialViewController];
    vc.rankItem = cell.rankItem;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
